import torch
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import time
import sys
sys.path.append("..")
from basic_knowledge import d2lzh_pytorch as d2l

#获取数据集
mnist_train = datasets.FashionMNIST('C:/ProgramData/Anaconda3/Lib/site-packages/torchvision/datasets',
                                                train=True, download=False,
                                                transform=transforms.ToTensor())
mnist_test = datasets.FashionMNIST('C:/ProgramData/Anaconda3/Lib/site-packages/torchvision/datasets',
                                               train=False, download=False,
                                               transform=transforms.ToTensor())
print(type(mnist_train))
print(len(mnist_train), len(mnist_test))

#一行显示十个图像
X, y = [], []
for i in range(10):
    X.append(mnist_train[i][0])
    y.append(mnist_train[i][1])
d2l.show_fashion_mnist(X, d2l.get_fashion_mnist_labels(y))

#读取数据
batch_size = 256
if sys.platform.startswith('win'):
    num_workers = 0  # 0表示不用额外的进程来加速读取数据
else:
    num_workers = 4
train_iter = torch.utils.data.DataLoader(mnist_train,
                                         batch_size=batch_size,
                                         shuffle=True,
                                         num_workers=num_workers)
test_iter = torch.utils.data.DataLoader(mnist_test,
                                        batch_size=batch_size,
                                        shuffle=False,
                                        num_workers=num_workers)

#查看读取的时间
start = time.time()
for X, y in train_iter:
    continue
print('%.2f sec' % (time.time() - start))